/*
 * @author : lj
 * @creation : 2014-6-10 下午3:20:00
 * @description : 
 *	我的停车场管理，用户：停车场管理员，可以添加停车场和维护停车场信息
 */

package com.action.park;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.base.UserBean;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.bean.ParkOrderBilling;
import com.bean.ParkOrderExt;
import com.bean.Parkinfo;
import com.bean.ParkinfoManagerRelevance;
import com.bean.Userinfo;
import com.dao.ParkOrderDao;
import com.service.ParkManagerService;
import com.service.ParkOrderService;
import com.service.ParkinfoService;
import com.util.GUtil;

public class MyOrderAction extends BaseParkAction {
	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	ParkOrderService service;
	ParkManagerService parkManagerService;

	public ParkOrderService getService() {
		return service;
	}

	public void setService(ParkOrderService service) {
		this.service = service;
	}

	public ParkOrder getOrder() {
		return order;
	}

	public void setOrder(ParkOrder order) {
		this.order = order;
	}

	ParkOrder order;
	List<ParkOrder> list;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);

	/**
	 * <p>
	 * Title: handle 默认处理方法
	 * </p>
	 * <p>
	 * Description: 默认处理方法
	 * </p>
	 * 
	 * @return see com.util.Util#
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#handle()
	 */

	@Override
	public String handle() throws Exception {
		if (id != null && id > 0)
			return this.view();
		return this.query();
	}

	@Override
	public String view() throws Exception {
		if (id == null || id < 1)
			return GUtil.ERROR;
		order = (ParkOrder) service.get(Long.valueOf(id));
		if (order == null)
			return GUtil.ERROR;
		return GUtil.VIEW;
	}

	public String getOrderList() throws Exception {
		ParkManager pm = (ParkManager) session.get("user");
		if (pm == null) {
			List<ParkOrder> orderList = service.getOrderList(pm.getUserId());
			session.put("orderList", orderList);
		} else {
			return "error";
		}
		return "orderList";
	}

	@Override
	public String add() throws Exception {
		this.setHeaderTitle("添加");
		if (order == null) {
			return GUtil.EDIT;
		}
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		order.setCreateTime(ts);
		order.setUpdateTime(ts);
		ParkinfoManagerRelevance pmr = new ParkinfoManagerRelevance();
		pmr.setUserId(user.getId());

		// if(service.add(parkInfo, pmr).endsWith(GUtil.SUCCESS)){
		// this.setMsg("添加成功");
		// parkInfo = null;
		// }else{
		// this.setMsg("添加失败");
		// }

		return GUtil.EDIT;
	}

	@Override
	public String update() throws Exception {
		this.setHeaderTitle("修改");
		// if(id==null){
		// this.setMsg("修改停车场信息异常");
		// return GUtil.ERROR;
		// }else if(order==null){
		// order = (Parkinfo) service.get(id);
		// if(order==null){
		// this.setLogmsg("信息不存在 id="+id);
		// return GUtil.ERROR;
		// }
		// if(order.getStatus()!=3){
		// this.setMsg("此条信息不可更改["+order.getStatusName()+"]");
		// return this.query();
		// }
		// return GUtil.EDIT;
		// }
		// Timestamp ts = new Timestamp(System.currentTimeMillis());
		// order.setUpdateTime(ts);
		// order.setPId(id);

		if (service.update(order).endsWith(GUtil.SUCCESS)) {
			this.setMsg("修改成功");
			order = null;
			return this.query();
		} else {
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}
	}

	/*
	 * 订单拒绝，修改状态为-1 (non-Javadoc)
	 * 
	 * @see com.action.park.BaseParkAction#delete()
	 */
	public String orderNo() throws Exception {
		if (id == null)
			return GUtil.ERROR;
		if (order == null) {
			// beforeUpdate
			order = (ParkOrder) service.get(Long.valueOf(id));
			if (order == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", order);
			session.put("id", order.getPoId());
		}
		// update
		ParkOrder pk = (ParkOrder) session.get("parkOrder");
		Short s = -1;
		pk.setStatus(s);
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		pk.setUpdateTime(ts);
		// to update
		if (service.update(pk).endsWith(GUtil.SUCCESS)) {
			this.setMsg("订单拒绝成功");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][订单拒绝成功," + "订单号:" + pk.getPoNo()
					+ "]");
			order = null;
			return this.query();
		} else {
			this.setMsg("订单拒绝失败");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][订单拒绝失败," + "订单号:" + pk.getPoNo()
					+ "]");
			return this.query();
		}
	}

	/*
	 * 确认收款 (non-Javadoc)
	 * 
	 * @see com.action.park.BaseParkAction#delete()
	 */
	public String okMonery() throws Exception {
		if (id == null)
			return GUtil.ERROR;
		if (order == null) {
			// beforeUpdate
			order = (ParkOrder) service.get(Long.valueOf(id));
			if (order == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", order);
			session.put("id", order.getPoId());
		}
		// update
		ParkOrder pk = (ParkOrder) session.get("parkOrder");
		Short s = 4;
		pk.setStatus(s);
		pk.setUpdateTime(ts);
		// to update

		if (service.update(pk).endsWith(GUtil.SUCCESS)) {
			this.setMsg("收款成功");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][已确认收款," + "订单号:" + pk.getPoNo()
					+ "]");
			order = null;
			return this.query();
		} else {
			this.setMsg("收款失败");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][收款失败," + "订单号:" + pk.getPoNo()
					+ "]");
			return this.query();
		}
	}

	/*
	 * 订单确认，修改状态为2 (non-Javadoc)
	 * 
	 * @see com.action.park.BaseParkAction#delete()
	 */
	public String Confirm() throws Exception {
		if (id == null)
			return GUtil.ERROR;
		if (order == null) {
			// beforeUpdate
			order = (ParkOrder) service.get(Long.valueOf(id));
			if (order == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", order);
			session.put("id", order.getPoId());
		}
		// update
		ParkOrder pk = (ParkOrder) session.get("parkOrder");
		Short s = 2;
		pk.setStatus(s);
		pk.setUpdateTime(ts);
		// to update
		if (service.update(pk).endsWith(GUtil.SUCCESS)) {
			this.setMsg("订单确认成功");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][确认订单成功," + "订单号:" + pk.getPoNo()
					+ "]");
			order = null;
			return this.query();
		} else {
			this.setMsg("订单确认失败");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][确认订单失败," + "订单号:" + pk.getPoNo()
					+ "]");
			return this.query();
		}

	}

	/**
	 * <p>
	 * Title: query 查询我的停车场信息
	 * </p>
	 * <p>
	 * Description: 查询我的停车场信息
	 * </p>
	 * 
	 * @return 我的停车场列表
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("预定管理");
		parkuser = (ParkManager) session.get("parkuser");
		if (parkuser == null)
			return GUtil.LOGIN;
		list = service.queryMyParkOrder(order, user,page);
		session.put("pname",service.getPname(user));
		session.put("status",service.getstatus());
		return GUtil.SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		if (id == null || id < 1)
			return GUtil.ERROR;
		else {
			if (service.delete(Long.valueOf(id), 4).equals(GUtil.SUCCESS))
				this.setMsg("结单成功");
			else
				this.setMsg("结单失败");
		}
		return this.query();//
	}

	/*
	 * 列为逃废
	 */
	public String Evasion() throws Exception {
		if (id == null){
			return GUtil.ERROR;
		}
		if (order == null) {
			// beforeUpdate
			order = (ParkOrder) service.get(Long.valueOf(id));
			if (order == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", order);
			session.put("id", order.getPoId());
		}
		// update

		user = (UserBean) session.get("user");
		ParkManager pm = parkManagerService.get(user.getId());// 收费员
		ParkOrder or = (ParkOrder) session.get("parkOrder");
		or.getPoId();// 订单id
		Userinfo userinfo = order.getUserinfo();// 司机
		or.setParkManagerByLeaveOperatorId(pm);
		Short s = 2;
		or.setStatus(s);
		or.setUpdateTime(ts);
		
		//计算价格
		ParkOrderExt poe = ParkOrderExt.newInstance( or.getExtendInfo());
		if(poe==null){
			this.setMsg("系统内部错误");
			log.error("ParkOrderExt.newInstance return null "+ this.getMsg());
			return GUtil.ERROR;
		}
		ParkOrderBilling pob = new ParkOrderBilling();
		or.setReceivableAmount( pob.CalcOrderBilling(poe, or.getOrderTime(), or.getLeaveTime()));
		service.updateuserandrecord(userinfo, or);
		if (service.update(or).endsWith(GUtil.SUCCESS)) {
			this.setMsg("成功列为逃废用户");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getUserinfo().getRealName() + "成功列为逃废用户," + "订单号:"
					+ or.getPoNo() + "]");
			or = null;
			userinfo = null;
			return this.query();
		} else {
			this.setMsg("列为逃废用户失败");
			this.setLogmsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getUserinfo().getRealName() + "列为逃废用户操作失败" + "订单号:"
					+ or.getPoNo() + "]");
			return this.query();
		}
	}
	

	/**
	 * @return the list
	 */
	public List<ParkOrder> getList() {
		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(List<ParkOrder> list) {
		this.list = list;
	}

	public ParkManagerService getParkManagerService() {
		return parkManagerService;
	}

	public void setParkManagerService(ParkManagerService parkManagerService) {
		this.parkManagerService = parkManagerService;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}
}
